<?php
/**
 * Created by PhpStorm.
 * User: lzh 9157374
 * Date: 2018/5/20
 * Time: 下午1:36
 */
namespace Core\common;

class Response
{
    /**
     * 返回json
     * @param $data
     */
    public static function json($data)
    {
        header('content-type:application/json');
        echo json_encode($data);
        exit;
    }

    /**
     * 输出图片
     * @param $file
     */
    public static function img($file)
    {
        try{
            if(!(strpos($file,'http://') === 0 || strpos($file,'https://') === 0)){
                $file = IA_ROOT.$file;
            }
            $context = stream_context_create(['http'=>['timeout'=>5]]);
            $imgData = file_get_contents($file,null,$context);
            $data    = getimagesizefromstring($imgData);
            if(!$data){
                exit('图片无效');
            }
            header('content-type:'.$data['mime']);
            ob_clean();
            echo $imgData;
        }catch (\Exception $e){
            echo '读取图片失败';
        }
        exit;
    }
}